<template>
  <div class="game-players-panel">
    <div class="panel-header">
      <h3 class="panel-title">👥 游戏玩家</h3>
      <span class="player-count">{{ players.length }}/{{ maxPlayers }}</span>
    </div>
    
    <div class="players-container">
      <div 
        v-for="player in sortedPlayers" 
        :key="player.id"
        class="player-card"
        :class="{
          'current-player': player.id === currentPlayerId,
          'host-player': player.isHost
        }"
      >
        <!-- 玩家头像 -->
        <div class="player-avatar-section">
          <div v-if="player.avatar" class="player-avatar">
            <img :src="player.avatar" :alt="player.name" class="avatar-img" />
          </div>
          <div v-else class="player-avatar-placeholder">
            <span class="avatar-icon">👤</span>
          </div>
          
          <!-- 房主标识 -->
          <div v-if="player.isHost" class="host-indicator">
            <span class="host-icon">👑</span>
          </div>
          
          
        </div>
        
        <!-- 玩家信息 -->
        <div class="player-details">
          <div class="player-name-section">
            <h4 class="player-name">{{ player.name }}</h4>
            <div class="player-badges">
              <span v-if="player.isHost" class="badge host-badge">房主</span>
              <span v-if="player.color" class="player-color-chip">
                <i class="chip-dot" :style="{ background: player.color }"></i>
                <span class="chip-text">{{ player.color }}</span>
              </span>
            </div>
          </div>
          
          <!-- 等级和称号 -->
          <div class="player-level-section">
            <div class="level-badge">
              <span class="level-icon">⭐</span>
              <span class="level-text">LV.{{ player.level || 1 }}</span>
            </div>
            <div class="title-badge">
              <span class="title-icon">🏆</span>
              <span class="title-text">{{ player.title || '墨痕初绽' }}</span>
            </div>
          </div>
          
        </div>
      </div>
    </div>
    
    
  </div>
</template>

<script>
import { computed } from 'vue'
import { useUserStore } from '@/stores/user'

export default {
  name: 'GamePlayersPanel',
  props: {
    players: {
      type: Array,
      default: () => []
    },
    maxPlayers: {
      type: Number,
      default: 5
    },
    gameStatus: {
      type: String,
      default: 'waiting'
    }
  },
  setup(props) {
    const userStore = useUserStore()
    
    const currentPlayerId = computed(() => userStore.user?.id)
    
    const readyCount = computed(() => {
      return props.players.filter(p => p.isReady).length
    })
    // 确保房主排在最前面
    const sortedPlayers = computed(() => {
      const arr = Array.isArray(props.players) ? [...props.players] : []
      return arr.sort((a, b) => {
        if (a?.isHost && !b?.isHost) return -1
        if (!a?.isHost && b?.isHost) return 1
        return 0
      })
    })
    
    return {
      currentPlayerId,
      readyCount,
      sortedPlayers
    }
  }
}
</script>

<style scoped>
.game-players-panel {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 2px solid #e5e7eb;
}

.panel-title {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  color: #111827;
}

.player-count {
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  color: white;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
}

.players-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.player-card {
  display: flex;
  gap: 16px;
  padding: 16px;
  border-radius: 12px;
  background: #f9fafb;
  border: 2px solid transparent;
  transition: all 0.3s ease;
}

.player-card.current-player {
  border-color: #3b82f6;
  background: #eff6ff;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.2);
}

.player-card.host-player {
  border-color: #f59e0b;
  background: #fffbeb;
}

/* 去掉准备态样式 */

.player-avatar-section {
  position: relative;
  flex-shrink: 0;
}

.player-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.player-avatar-placeholder {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, #e5e7eb, #d1d5db);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 3px solid white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.avatar-icon {
  font-size: 24px;
  color: #6b7280;
}

.host-indicator {
  position: absolute;
  top: -5px;
  right: -5px;
  background: linear-gradient(135deg, #fbbf24, #f59e0b);
  color: white;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  border: 2px solid white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.ready-indicator {
  position: absolute;
  bottom: -5px;
  right: -5px;
  background: linear-gradient(135deg, #10b981, #059669);
  color: white;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  border: 2px solid white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.player-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.player-name-section {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.player-name {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  color: #111827;
}

.player-badges {
  display: flex;
  gap: 6px;
}

.player-color-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  border-radius: 12px;
  background: #ffffff;
  border: 1px solid #e5e7eb;
}
.player-color-chip .chip-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px #e5e7eb;
}
.player-color-chip .chip-text {
  font-size: 11px;
  color: #374151;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

.badge {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
}

.host-badge {
  background: linear-gradient(135deg, #fbbf24, #f59e0b);
  color: white;
}

.ready-badge {
  background: linear-gradient(135deg, #10b981, #059669);
  color: white;
}

.player-level-section {
  display: flex;
  gap: 8px;
}

.level-badge, .title-badge {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}

.level-badge {
  background: linear-gradient(135deg, #fbbf24, #f59e0b);
  color: white;
}

.title-badge {
  background: linear-gradient(135deg, #8b5cf6, #7c3aed);
  color: white;
}

.level-icon, .title-icon {
  font-size: 12px;
}

.player-color-section {
  display: flex;
  align-items: center;
  gap: 8px;
}

.color-label {
  font-size: 12px;
  color: #6b7280;
}

.color-preview {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.color-value {
  font-size: 12px;
  font-weight: 600;
  color: #374151;
  font-family: monospace;
}

.player-stats {
  display: flex;
  gap: 16px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid #e5e7eb;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.stat-label {
  font-size: 10px;
  color: #6b7280;
  text-transform: uppercase;
}

.stat-value {
  font-size: 14px;
  font-weight: 700;
  color: #111827;
}

.game-status-tip {
  margin-top: 20px;
  padding: 16px;
  background: linear-gradient(135deg, #fef3c7, #fde68a);
  border-radius: 12px;
  border: 1px solid #f59e0b;
}

.tip-content {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.tip-icon {
  font-size: 16px;
}

.tip-text {
  font-size: 14px;
  color: #92400e;
  font-weight: 500;
}

.tip-count {
  background: #f59e0b;
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .game-players-panel {
    padding: 16px;
  }
  
  .player-card {
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
  
  .player-avatar-section {
    align-self: center;
  }
  
  .player-name-section {
    justify-content: center;
    flex-direction: column;
    gap: 8px;
  }
  
  .player-level-section {
    justify-content: center;
  }
  
  .player-color-section {
    justify-content: center;
  }
  
  .player-stats {
    justify-content: center;
  }
}
</style>
